package personalPage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.adapter.CollegeIndexVolunteerListAdapter;
import mainFragment.bean.VolunteerListBean;
import view.MyPtrRefresh_LoadView;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/4/6.
 * 他关注的志愿者
 */
@ContentView(R.layout.volunteerlist)
public class VolunteerList extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView back;
    @ViewInject(R.id.common_title)
    private TextView title;
    @ViewInject(R.id.volunteerlist_refreshLayout)
    private MyPtrRefresh_LoadView myRefresh_loadLayout;
    @ViewInject(R.id.volunteerlist_lv)
    private ListView listView;

    private ArrayList<VolunteerListBean.VolunteerListbean> arrayList1;
    private CollegeIndexVolunteerListAdapter collegeIndexVolunteerListAdapter;

    private int page;
    private static final int REFRESH = 0;
    private static final int LOAD = 1;
    private String userid;
    private String from;
    private MyHandler handler;

    private static class MyHandler extends Handler {
        WeakReference<VolunteerList> weakReference;

        public MyHandler(VolunteerList weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            VolunteerList volunteerList = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    VolunteerListBean mainIndexBean = gson.fromJson((String) msg.obj, VolunteerListBean.class);
                    if (mainIndexBean != null) {
                        List<VolunteerListBean.VolunteerListbean> expertList = mainIndexBean.getVolunteerList();//获得志愿者
                        if (expertList != null) {
                            switch (msg.arg1) {
                                case REFRESH:
                                    if (expertList.size() > 0) {
                                        volunteerList.arrayList1.clear();
                                        for (int i = 0; i < expertList.size(); i++) {
                                            volunteerList.arrayList1.add(expertList.get(i));
                                        }
                                    }
                                    break;
                                case LOAD:
                                    if (expertList.size() > 0) {
                                        for (int i = 0; i < expertList.size(); i++) {
                                            volunteerList.arrayList1.add(expertList.get(i));
                                        }
                                    } else {
                                        --volunteerList.page;
                                    }
                                    break;
                                default:
                                    break;
                            }
                            volunteerList.collegeIndexVolunteerListAdapter.notifyDataSetChanged();

                        }
                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    volunteerList.myRefresh_loadLayout.OnRefreshComplete();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userid = getIntent().getStringExtra("userId");
        from = getIntent().getStringExtra("from");
        handler = new MyHandler(this);
        initView();
        getDataFromInterNet(REFRESH);
    }

    private void initView() {
        page = 1;
        arrayList1 = new ArrayList<>();
        collegeIndexVolunteerListAdapter = new CollegeIndexVolunteerListAdapter(this, arrayList1);
        listView.setAdapter(collegeIndexVolunteerListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(VolunteerList.this, Volunteer.class);
                intent.putExtra("type", "vip");
                intent.putExtra("id", arrayList1.get(position).getId());
                startActivity(intent);
            }
        });
        back.setOnClickListener(this);
        title.setText("关注高校百事通");
        myRefresh_loadLayout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page++;
                getDataFromInterNet(LOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                getDataFromInterNet(REFRESH);
            }
        });
    }

    private void getDataFromInterNet(int flag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("userid", userid);
        hashMap.put("type", "expert");
        hashMap.put("from", from);
        hashMap.put("page", String.valueOf(page));
        new InterNetController(this, Constant.PERSONALDETAIL, handler, hashMap, flag, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            default:
                break;
        }
    }
}
